package com.example.android.supermemo.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Dumrongrith on 8/5/2014.
 */


public class MainFragment extends Fragment {

    private final String LOG_TAG = MainActivity.class.getSimpleName();

    final static int RESULT_SPEECH = 1;
    private String STT_LANGUAGE;

    Button sayButton , speakButton;
    EditText sst_resultText;
    ListView lv;

    public MainFragment() {
    }

    @Override
    public void onResume() {
        super.onResume();
        if (STT_LANGUAGE != null && !STT_LANGUAGE.equals(Utility.getPreferredLanguage(getActivity()))) {
            STT_LANGUAGE = Utility.getPreferredLanguage(getActivity());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        STT_LANGUAGE = Utility.getPreferredLanguage(getActivity());
        sst_resultText = (EditText) rootView.findViewById(R.id.editText);
        sayButton = (Button) rootView.findViewById(R.id.say_button);
        speakButton = (Button) rootView.findViewById(R.id.speak_button);


        sayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =
                        new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, STT_LANGUAGE);
                startActivityForResult(intent, RESULT_SPEECH);
            }
        });

        speakButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sst_resultText.getText().toString().trim().length() == 0) {
                    Toast.makeText(getActivity(), "Please enter your message.", Toast.LENGTH_LONG).show();
                    return;
                }
                TTSTask ttsTask = new TTSTask();
                ttsTask.execute(Utility.getPreferredLanguage(getActivity()),sst_resultText.getText().toString());

            }
        });

        return rootView;
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_SPEECH && data != null) {
            ArrayList<String> arr_data =
                    data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            final String[] arr_str = new String[arr_data.size()];
            for(int i = 0 ; i < arr_str.length ; i++)
                arr_str[i] = arr_data.get(i);

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Recognize result");
            builder.setItems(arr_str, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    sst_resultText.setText(arr_str[which]);
                }
            });
            builder.show();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}